﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Data.Entity;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using HdipCricketClub.Models;

namespace HdipCricketClub.Controllers
{
    public class FixtureController : Controller
    {
        private CricketClubEntities db = new CricketClubEntities();

        FixtureRepository fixtureRepository = new FixtureRepository();

        //
        //  GET: /Fixture/List/

        public ViewResult List(string sortOrder, string searchString)
        {
            ViewBag.fixtureSortParm = sortOrder;
            var fixtures = fixtureRepository.FindAllFixtures();

            if (!String.IsNullOrEmpty(searchString))
            {
                fixtures = fixtures.Where(f => f.Team.name.ToUpper().Contains(searchString.ToUpper()));
            }
            return View(fixtures.ToList());
        }

        //
        // GET: /Fixture/

        public ViewResult Index(string sortOrder, string searchString)
        {
            ViewBag.DateSortParm = sortOrder;
            var fixtures = fixtureRepository.FindAllFixtures();

            if (!String.IsNullOrEmpty(searchString))
            {
                fixtures = fixtures.Where(f => f.Team.name == searchString);
            }

            return View(fixtures.ToList());
        }

        //
        // GET: /Fixture/Details/5

        public ViewResult Details(int id)
        {
            Fixture fixture = fixtureRepository.GetFixture(id);
            return View(fixture);
        }

        //
        // GET: /Fixture/Create

        public ActionResult Create()
        {
            ViewBag.team_id = new SelectList(db.Teams, "team_id", "name");
            ViewBag.opponent_id = new SelectList(db.Opponents, "opponent_id", "club_name");
            return View();
        }

        //
        // POST: /Fixture/Create

        [HttpPost]
        public ActionResult Create(Fixture fixture)
        {
            if (ModelState.IsValid)
            {
                db.Fixtures.AddObject(fixture);
                db.SaveChanges();
                return RedirectToAction("Index");
            }

            ViewBag.team_id = new SelectList(db.Teams, "team_id", "name", fixture.team_id);
            ViewBag.opponent_id = new SelectList(db.Opponents, "opponent_id", "club_name", fixture.opponent_id);
            return View(fixture);
        }

        //
        // GET: /Fixture/Edit/5

        public ActionResult Edit(int id)
        {
            Fixture fixture = fixtureRepository.GetFixture(id);
            ViewBag.team_id = new SelectList(db.Teams, "team_id", "name", fixture.team_id);
            ViewBag.opponent_id = new SelectList(db.Opponents, "opponent_id", "club_name", fixture.opponent_id);
            return View(fixture);
        }

        //
        // POST: /Fixture/Edit/5

        [HttpPost]
        public ActionResult Edit(Fixture fixture)
        {
            if (ModelState.IsValid)
            {
                db.Fixtures.Attach(fixture);
                db.ObjectStateManager.ChangeObjectState(fixture, EntityState.Modified);
                db.SaveChanges();
                return RedirectToAction("Index");
            }
            ViewBag.team_id = new SelectList(db.Teams, "team_id", "name", fixture.team_id);
            ViewBag.opponent_id = new SelectList(db.Opponents, "opponent_id", "club_name", fixture.opponent_id);
            return View(fixture);
        }

        //
        // GET: /Fixture/Delete/5

        public ActionResult Delete(int id)
        {
            Fixture fixture = fixtureRepository.GetFixture(id);
            return View(fixture);
        }

        //
        // POST: /Fixture/Delete/5

        [HttpPost, ActionName("Delete")]
        public ActionResult DeleteConfirmed(int id)
        {
            try
            {
                Fixture fixture = fixtureRepository.GetFixture(id);
                db.Fixtures.DeleteObject(fixture);
                db.SaveChanges();
                return RedirectToAction("Index");
            }
            catch (Exception)
            {
                Fixture fixture = fixtureRepository.GetFixture(id);
                return RedirectToAction("UpdateError", new { id = fixture.fixture_id });
            }
        }

        protected override void Dispose(bool disposing)
        {
            db.Dispose();
            base.Dispose(disposing);
        }

        //
        //  GET Fixture/UpdateError/

        public ActionResult UpdateError(int id)
        {
            Fixture fixture = fixtureRepository.GetFixture(id);

            return View(fixture);
        }



    }
}